<?php

/**
 * 房源拍卖信息
 */

namespace app\crontab\model;

use app\common\lib\RespCode;
use app\common\model\BaseModel;
use think\Exception;

class HouseAuction extends BaseModel {

    protected $autoWriteTimestamp = true;
    protected $createTime = 'itime';
    protected $updateTime = 'utime';

    /**
     * 更新房源拍卖信息
     * @param $params
     * @return bool
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(&$params) {

        $params['is_bind_house'] = true;
        $old = $this->where(['source_id' => $params['source_id']])->find();
        if (empty($old)) {
            $this->strict(false)->insert($params);
            $params['auction_id'] = $this->getLastInsID();
            return true;
        }
        $params['soure_updatetime'] = time();
        $params['is_bind_house'] = false;
        if (!$this->allowField(true)->isUpdate(true)->save($params, ['id' => $old->id])) {
            throw new Exception('更新拍卖信息失败', RespCode::PARAMS_ERR);
        }

        $params['auction_id'] = $old->id;
        return true;
    }

    /**
     * 绑定房源信息
     * @param $params
     * @return bool
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function bindHouseId(&$params) {
        if ($params['is_bind_house']) {
            $uData = ['house_id' => $params['house_id']];
            $where = "`id` = '{$params['auction_id']}'";
            if (!$this->customName('erp_house_auction')->customUpdate($uData, $where)) {
                throw new Exception('拍卖信息绑定房源id失败', RespCode::PARAMS_ERR);
            }

        }
        return true;
    }

}